import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:yinshui_caiying/http/apis.dart';
import 'package:yinshui_caiying/http/http.dart';
import 'package:yinshui_caiying/model/loginInfo_model.dart';
import 'package:yinshui_caiying/model/result_model.dart';
import 'package:yinshui_caiying/model/userInfo_result_model.dart';
import 'package:yinshui_caiying/pages/login.dart';
import 'package:yinshui_caiying/services/storage_services.dart';
import 'dart:convert';

import 'package:yinshui_caiying/widgets/my_dialog.dart';

///用户服务
class UserService extends GetxService {
  Future<UserService> init() async {
    return this;
  }

  @override
  void onInit() {
    getLoginInfoData();
    getUserInfoData();
    super.onInit();
  }

  ///用户信息
  final userinfo = UserInfoModel(
          id: 0,
          name: '',
          account: '',
          email: '',
          sex: '',
          phone: '',
          orgId: 0,
          orgName: '',
          controlPwd: '',
          adId: 0,
          adName: '')
      .obs;

  ///登录信息
  final logininfo =
      LoginInfoModel(username: "", password: '', rememberPassword: false).obs;

  ///用户名控制器
  TextEditingController username_c = TextEditingController();

  ///密码框控制器
  TextEditingController password_c = TextEditingController();

  ///读取本地用户信息
  getUserInfoData() async {
    var data = await Storage.getString('userinfo');
    if (data != null) {
      //有数据
      Map<String, dynamic> res = json.decode(data);
      userinfo(UserInfoModel.fromJson(res));
    }
  }

  ///设置用户信息并存储本地
  ///
  ///[data]用户信息,Map类型
  setUserInfoData({required UserInfoModel data}) async {
    userinfo(data);
    await Storage.setString('userinfo', json.encode(data));
  }

  ///移除本地用户信息
  removeUserInfoData() async {
    await Storage.remove('userinfo');
    userinfo(UserInfoModel(
        id: 0,
        name: '',
        account: '',
        email: '',
        sex: '',
        phone: '',
        orgId: 0,
        orgName: '',
        controlPwd: '',
        adId: 0,
        adName: ''));
  }

  ///读取本地登录信息
  getLoginInfoData() async {
    var data = await Storage.getString('logininfo');
    if (data != null) {
      //有数据
      Map<String, dynamic> res = json.decode(data);
      logininfo(LoginInfoModel.fromJson(res));
      username_c.text = logininfo.value.username;
      password_c.text = logininfo.value.password;
    }
  }

  ///设置本地登录信息
  setLoginInfoData() async {
    try {
      await Storage.setString(
          'logininfo', json.encode(logininfo.value.toJson()));
    } catch (e) {
      print(e);
    }
  }

  ///移除本地登录信息
  removeLoginInfoData() async {
    await Storage.remove('logininfo');
    logininfo(LoginInfoModel(
      username: "",
      password: '',
      rememberPassword: false,
    ));
  }

  loginOut() async {
    MyDialog.showLoading(msg: '注销中...');

    try {
      ResultModel result =
          ResultModel.fromJson(await Http().post(ApiSystem.SYSTEM_USER_LOGOUT));

      if (result.code == 200) {
        SmartDialog.dismiss();
        removeLoginInfoData();
        removeUserInfoData();
        Get.offAll(() => LoginPage());
      }
    } catch (e) {
      SmartDialog.dismiss();
    }
  }
}
